package utilities;

import java.io.File;

//first convert byte array to String array, and then parse data based on String array

public class DataParsing2 {

	public static void main(String[] args) {
		File dir = new File("C:\\Documents and Settings\\829929\\cadDump\\june22\\");
		File[] files = dir.listFiles();
		byte[] data;
		String fileName;
		int counter=0;
		for(int i=0; i < files.length; i++){
			fileName = "C:\\Documents and Settings\\829929\\cadDump\\june22\\";
			fileName = fileName + files[i].getName();
			data = ReadNWriteAFile.readFile3(fileName);
			try{
				System.out.println("awb: " + getAWB_NBR(data));
				System.out.println("awbDate: " + getAWBDate(data));
				System.out.println("awbTime: " + getAWBTime(data));
				System.out.println("getOriginCityCD " + getOriginCityCD(data));
				System.out.println("carrier: " + getCarrierCD(data));
			}
			catch(StringIndexOutOfBoundsException e){
				System.out.println("error: " + fileName);
				String[] strarr = new String(ReadNWriteAFile.readFile3(fileName)).split("\\s");
				System.out.println("awb error: " + strarr[10]);
				counter++;
				System.out.println("counter: " + counter);
			}
			/*System.out.println("awbDate: " + strarr[11]);
			System.out.println("awbTime: " + strarr[12]);
			System.out.println("terminator: " + strarr[strarr.length-1]);
			tmp = strarr[strarr.length-1];
			if(tmp.endsWith(">+")||tmp.endsWith(">+"))
				System.out.println("error: " + fileName);*/
		}//end for
		
		//data = ReadNWriteAFile.readFile3("C:\\Documents and Settings\\829929\\cadDump\\june17\\");
	}
	
	public static String getCarrierCD(byte[] data){
		String carrier="";
		//convert byte array to String
		String str = new String(data);
		//convert String to String array separating by space
		String[] strarr = str.split("\\s");
		//substring(startIndex) or substring(startIndex, endIndex);
		for(String item: strarr){
			if(item.startsWith("AIRL/"))
				carrier = item;
		}
		return carrier;
	}
	
	public static String getOriginCityCD(byte[] data){
		//convert byte array to String
		String str = new String(data);
		//convert String to String array separating by space
		String[] strarr = str.split("\\s");
		//substring(startIndex) or substring(startIndex, endIndex);
		return strarr[10].substring(3,6);
	}
	
	public static String getAWB_NBR(byte[] data){
		//convert byte array to String
		String str = new String(data);
		//convert String to String array separating by space
		String[] strarr = str.split("\\s");
		//substring(startIndex) or substring(startIndex, endIndex);
		return strarr[10].substring(6, 14);
		//return strarr[10];
	}
	
	public static String getAWBDate(byte[] data){
		//convert byte array to String
		String str = new String(data);
		//convert String to String array separating by space
		String[] strarr = str.split("\\s");
		//substring(startIndex) or substring(startIndex, endIndex);
		//return strarr[11];
		return dateFormat(strarr[11]);
		
	}
	private static String dateFormat(String str){
		return str = str.substring(0, 2)+"-"+str.substring(2, 4)+"-"+str.substring(4, 6);
	}
	public static String getAWBTime(byte[] data){
		//convert byte array to String
		String str = new String(data);
		//convert String to String array separating by space
		String[] strarr = str.split("\\s");
		//substring(startIndex) or substring(startIndex, endIndex);
		//return strarr[12];
		return timeFormat(strarr[12]);
	}
	private static String timeFormat(String str){
		return str = str.substring(0, 2)+":"+str.substring(2, 4);
	}
	
	public static String getMetaData(byte[] data){
		String flag = "completed";
		String str = new String(data);
		if(!str.contains(">+")){
			flag = "incompleted";
		}
		return flag;
	}

}
